#ifndef IMAGEDIR_READER_H_
#define IMAGEDIR_READER_H_


#include <dirent.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/time.h>

#include <iostream>
#include <string>
#include <vector>
#include <thread>
#include <algorithm>  

#include "atlas_videocapture.h"
#include "dvpp_process.h"
#include "thread_safe_queue.h"
#include "utils.h"

enum ReaderStatus {
    READER_ERROR  = -1,
    READER_UNINIT = 0,
    READER_READY  = 1,
    READER_START  = 2,
    READER_DIR_FINISHIED = 3,
    READER_FINISHED = 4
};

class ImageDirReader : public AtlasVideoCapBase {

public:
    /**
     * @brief VideoDecode constructor
     */
    ImageDirReader(const std::string& dirName, aclrtContext context = nullptr);

    /**
     * @brief VideoDecode destructor
     */
    ~ImageDirReader();

    static void FrameReadThreadFunction(void* readerSelf); 

    bool IsOpened();

    AtlasError Open();

    void SetStatus(ReaderStatus status) { status_ = status; }
    ReaderStatus GetStatus() { return status_; }

    AtlasError Set(StreamProperty key, uint32_t value) { return ATLAS_OK; }
    uint32_t Get(StreamProperty key);

    void SleeptoNextFrameTime();
    AtlasError SetAclContext();

    void ProcessDecodedImage(shared_ptr<ImageData> frameData);
    AtlasError Read(ImageData& frame);

    AtlasError Close();

    void DestroyResource();
    bool IsStop() { return isStop_; }

private:
    AtlasError InitResource();
    void ImageRead(ImageDirReader* thisPtr);
    AtlasError FrameImageEnQueue(shared_ptr<ImageData> frameData);
    shared_ptr<ImageData> FrameImageOutQueue(bool noWait = false);
    void StartFrameReader();
    

private:
    aclrtContext context_;
    aclrtStream stream_;
    aclrtRunMode runMode_;

    uint32_t frameId_;
    uint32_t finFrameCnt_;
    ReaderStatus status_;
    std::string streamName_;
    int64_t lastReadTime_;
    int64_t fpsInterval_;
    thread readerThread_;

    uint32_t imageWidth_;
    uint32_t imageHeight_;
    uint32_t fps_;

    DvppProcess dvpp_;

    ThreadSafeQueue<shared_ptr<ImageData>> frameImageQueue_;
    
    bool isStop_;
    bool isReleased_;
};

#endif /* IMAGEDIR_READER_H_ */
